<template>
	<view class="login-container">
		<!-- 顶部Logo区域 -->
		<view class="logo-area">
			<image class="logo" src="/static/logo.png" mode="aspectFit"></image>
			<text class="title">维修工单系统</text>
		</view>

		<!-- 登录表单 -->
		<view class="form-container">
			<view class="input-item">
				<uni-icons type="person" size="20" color="#999"></uni-icons>
				<input type="number" v-model="form.mobile" placeholder="请输入手机号" maxlength="11" />
			</view>

			<view class="input-item">
				<uni-icons type="locked" size="20" color="#999"></uni-icons>
				<input type="password" v-model="form.password" placeholder="请输入密码" />
			</view>

			<button class="login-btn" @click="handleLogin">登录</button>

			<view class="register-link">
				<text @click="goToRegister">还没有账号？立即注册</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		setToken
	} from '@/utils/auth.js'

	export default {
		data() {
			return {
				form: {
					mobile: '',
					password: ''
				}
			}
		},
		methods: {
			async handleLogin() {
				// 表单验证
				if (!this.form.mobile || !/^1\d{10}$/.test(this.form.mobile)) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				if (!this.form.password) {
					uni.showToast({
						title: '请输入密码',
						icon: 'none'
					})
					return
				}

				try {
					console.log('开始登录请求，参数:', this.form)
					const res = await this.$http.post('/api/login', this.form)
					console.log('登录响应:', res)

					if (res.code === 0) {
						// 检查token是否存在
						if (!res.data.token) {
							console.error('登录响应中没有token')
							uni.showToast({
								title: '登录失败：未获取到token',
								icon: 'none'
							})
							return
						}

						// 打印token和过期时间
						console.log('获取到的token:', res.data.token)
						console.log('获取到的过期时间:', res.data.expire)

						// 保存token和用户信息
						console.log('保存登录信息')
						setToken(res.data.token, res.data.expire)
						uni.setStorageSync('userInfo', res.data.user)

						// 设置用户类型
						const app = getApp()
						if (app && app.$vm && app.$vm.$refs && app.$vm.$refs.customTabBar) {
							app.$vm.$refs.customTabBar.setUserType(res.data.user.userType === 0 ? 'student' : 'maintainer')
						}

						// 登录成功后跳转到首页
						uni.switchTab({
							url: '/pages/index/index',
							success: () => {
								uni.showToast({
									title: '登录成功',
									icon: 'success'
								})
							}
						})
					} else {
						console.error('登录失败:', res.msg)
						uni.showToast({
							title: res.msg || '登录失败',
							icon: 'none'
						})
					}
				} catch (error) {
					console.error('登录异常:', error)
					uni.showToast({
						title: '登录失败，请重试',
						icon: 'none'
					})
				}
			},

			goToRegister() {
				uni.navigateTo({
					url: '/pages/register/index'
				})
			}
		}
	}
</script>

<style>
	.login-container {
		min-height: 100vh;
		padding: 60rpx 40rpx;
		background-color: #fff;
	}

	.logo-area {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 80rpx;
	}

	.logo {
		width: 160rpx;
		height: 160rpx;
		margin-bottom: 20rpx;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	.form-container {
		margin-top: 60rpx;
	}

	.input-item {
		display: flex;
		align-items: center;
		height: 100rpx;
		padding: 0 30rpx;
		margin-bottom: 30rpx;
		background-color: #f8f8f8;
		border-radius: 50rpx;
	}

	.input-item uni-icons {
		margin-right: 20rpx;
	}

	.input-item input {
		flex: 1;
		font-size: 28rpx;
	}

	.login-btn {
		width: 100%;
		height: 90rpx;
		line-height: 90rpx;
		margin-top: 60rpx;
		background-color: #2B65FE;
		color: #fff;
		font-size: 32rpx;
		border-radius: 45rpx;
	}

	.register-link {
		margin-top: 30rpx;
		text-align: center;
	}

	.register-link text {
		color: #2B65FE;
		font-size: 28rpx;
	}
</style>